package com.battleship.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.battleship.R;
import com.battleship.modelo.Barco;
import com.battleship.utils.Constants;

public class LocalGameSelectView extends View {

	protected Drawable[][] table = new Drawable[Constants.TABLE_WIDTH][Constants.TABLE_HEIGHT];

	protected Barco[] ships = new Barco[Constants.SHIPS_COUNT];

	protected int marginTop = Constants.BOX_SIZE/2;
	protected int marginLeft = Constants.BOX_SIZE;

	public LocalGameSelectView(Context context) {
		super(context);
		init();
	}

	private void init() {

		for (int w = 0; w < Constants.TABLE_WIDTH; w++) {
			for (int h = 0; h < Constants.TABLE_HEIGHT; h++) {
				Drawable drawable = getResources().getDrawable(
						R.drawable.tablebox);
				int left = marginLeft + (w * Constants.BOX_SIZE);
				int top = marginTop + (h * Constants.BOX_SIZE);
				int right = left + Constants.BOX_SIZE;
				int bottom = top + Constants.BOX_SIZE;
				drawable.setBounds(left, top, right, bottom);
				table[w][h] = drawable;
			}
		}

		int shipsMarginTop = Constants.BOX_SIZE;
		int shipsMarginLeft = marginLeft + (Constants.BOX_SIZE * Constants.TABLE_WIDTH)
				+ marginLeft;
		int i = 0;
		for (i = 0; i < Constants.SHIPS_LENGTH_2; i++) {
			Barco barco = new Barco();
			int left = shipsMarginLeft;
			int top = shipsMarginTop + (i * 2 * Constants.BOX_SIZE);
			int right = left + 2*Constants.BOX_SIZE;
			int bottom = top + Constants.BOX_SIZE;
			barco.setBoundLeft(left);
			barco.setBoundTop(top);
			barco.setBoundRight(right);
			barco.setBoundBottom(bottom);
			barco.setLongitud(2);
			ships[i] = barco;
		}
		for (int k = 0; k < Constants.SHIPS_LENGTH_3; i++, k++) {
			Barco barco = new Barco();
			int left = shipsMarginLeft;
			int top = shipsMarginTop + (i * 2 * Constants.BOX_SIZE);
			int right = left + 3*Constants.BOX_SIZE;
			int bottom = top + Constants.BOX_SIZE;
			barco.setBoundLeft(left);
			barco.setBoundTop(top);
			barco.setBoundRight(right);
			barco.setBoundBottom(bottom);
			barco.setLongitud(3);
			ships[i] = barco;
		}
		for (int k = 0; k < Constants.SHIPS_LENGTH_4; i++, k++) {
			Barco barco = new Barco();
			int left = shipsMarginLeft;
			int top = shipsMarginTop + (i * 2 * Constants.BOX_SIZE);
			int right = left + 4*Constants.BOX_SIZE;
			int bottom = top + Constants.BOX_SIZE;
			ships[i] = barco;
			barco.setBoundLeft(left);
			barco.setBoundTop(top);
			barco.setBoundRight(right);
			barco.setBoundBottom(bottom);
			barco.setLongitud(4);
		}

	}

	public Object getPositionObject(int x, int y) {
		//View all map objects
		for(Barco barco: ships) {
			if(barco.isIn(x,y)) {
				return barco;
			}
		}
		return null;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		for (int w = 0; w < Constants.TABLE_WIDTH; w++) {
			for (int h = 0; h < Constants.TABLE_HEIGHT; h++) {
				table[w][h].draw(canvas);
			}
		}
		for (int i = 0; i < Constants.SHIPS_COUNT; i++) {
			this.drawShip(this.ships[i], canvas);
		}
	}

	private void drawShip(Barco barco, Canvas canvas) {
		for (int i = 0; i < barco.getLongitud(); i++) {
			Drawable drawable = getResources().getDrawable(R.drawable.shipbox);
			int left;
			int top;
			int right;
			int bottom;
			if ((barco.getBoundLeft() - barco.getBoundRight()) > Constants.BOX_SIZE
					|| (barco.getBoundLeft() - barco.getBoundRight()) < (-Constants.BOX_SIZE)) {
				// Barco en horizontal
				left = barco.getBoundLeft() + i * Constants.BOX_SIZE;
				top = barco.getBoundTop();
				right = left + Constants.BOX_SIZE;
				bottom = barco.getBoundBottom();
			} else {
				// Barco en vertical
				left = barco.getBoundLeft();
				top = barco.getBoundTop() + (i * Constants.BOX_SIZE);
				right = barco.getBoundRight();
				bottom = top + Constants.BOX_SIZE;
			}
			drawable.setBounds(left, top, right, bottom);
			drawable.draw(canvas);
		}
	}

	public void setTableDrawable(int x, int y, Drawable drawable) {
		this.table[x][y] = drawable;
	}

}
